package com.wing.ext.controller;

import java.io.IOException;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.wing.cms.domain.Artgroup;
import com.wing.common.Constants;
import com.wing.ext.domain.Productgroup;
import com.wing.ext.domain.Products;
import com.wing.ext.service.ProductgroupManager;
import com.wing.ext.service.ProductsManager;

/**
 * 
 * 
 * @author wyf
 * 
 */
 
@Controller
@RequestMapping(value = "/ext/productgroup")
public class ProductgroupController {

	@Autowired
	private ProductgroupManager productgroupManager;
	@Autowired
	private ProductsManager productsManager;
	
	
	/**
	 * 转发路径
	 *  
	 */
	@RequestMapping(value = "get/{url}", method = RequestMethod.GET)
	public String forward(@PathVariable String url) {
		return "/ext/productgroup/"+url;
	}
	

	/**
	 * 是根据文章组parentId获取的文章组,生成树
	 * 
	 * @param request
	 * @param response
	 * @param id
	 * @return
	 *  
	 * @throws ServletException, IOException
	 */
	@RequestMapping(value = "showTree", method = RequestMethod.GET)
	public void showTree(HttpServletRequest request,
			HttpServletResponse response) {
		String siteId = (String)request.getSession().getAttribute(Constants.SESSION_SITE_ID);
		response.setContentType("text/xml;charset=UTF-8");
		response.setHeader("Pragma", "No-cache");//HTTP 1.1    
		response.setHeader("Cache-Control", "no-cache");//HTTP 1.0    
		response.setHeader("Expires", "0");//防止被proxy
		
		String pid = request.getParameter("id");
		boolean firstFlag = false;
		if (pid == null || pid.equalsIgnoreCase("")) {
			pid = Artgroup.ARTGROUPROOT;//我数据库中一级节点的parentid都是-1
			firstFlag = true;
		}
		try {
			response.getWriter()
					.write(productgroupManager.createDHTMLXML(pid,firstFlag,siteId));
			response.getWriter().close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	
	/**
	 * 打开编辑页面
	 * @param id
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "toForm/{id}", method = RequestMethod.GET)
	public String toEditPage(@PathVariable String id,String pid, Model model) {
		Productgroup productgroup = null;
		if(!id.equals("0")){
			productgroup = productgroupManager.findById(id);
		}else{
			productgroup = new Productgroup();
			productgroup.setPid(pid);
		}
		model.addAttribute("productgroup", productgroup);
		return "/ext/productgroup/productgroupForm";
	}
	
	
	@RequestMapping(value = "save", method = RequestMethod.POST)
	public  @ResponseBody  String save(Productgroup productgroup) {
		return productgroupManager.saveProductgroup(productgroup);
	}
	
	@RequestMapping(value = "delproductgroup/{id}", method = RequestMethod.POST)
	public @ResponseBody String delproductgroup(@PathVariable String id) throws IOException {
		if("ROOT".equals(id)){
			return Constants.DEFAULT_AJAX_FAILURE;
		}
		productgroupManager.deleteProductgroups(id);
		return Constants.DEFAULT_AJAX_SUCCESS;
	}
	


	/**
	 * 是根据文章组parentId获取的文章组,生成树
	 * 
	 * @param request
	 * @param response
	 * @param id
	 * @return
	 *  
	 * @throws ServletException, IOException
	 */
	@RequestMapping(value = "lst", method = RequestMethod.POST)
	public @ResponseBody List list(HttpServletRequest request,String pid) {
		String siteId = (String)request.getSession().getAttribute(Constants.SESSION_SITE_ID);

		List list = productgroupManager.findChildByID(pid,siteId);
		return list;
	}
	/**
	 * 获取pid下的所有子产品组，和该组下的第一个产品。
	 * @param request
	 * @param pid
	 * @return
	 */
	@RequestMapping(value = "getgrpsfp", method = RequestMethod.POST)
	public @ResponseBody List getgrpsfp(HttpServletRequest request,String pid) {
		String siteId = (String)request.getSession().getAttribute(Constants.SESSION_SITE_ID);
		List list = productgroupManager.findChildByID(pid,siteId);
		for(int i=0;i<list.size();i++){
			Map map = (Map)list.get(i);
			Map product = productsManager.getFistProductsByGrpId((String)map.get("id"));
			map.put("firstProducts", product);
		}
		return list;
	}
}

